package com.spera.shopoa.util;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.spring.common.web.service.CommonService;
import com.spring.nbrbt.tools.otherutil.SpringContextHolder;
import com.spring.security.core.context.JedisCont;
import com.spring.security.core.util.ApplicationUtils;
import com.spring.security.core.util.CookieUtils;
import com.spring.security.web.iputil.HttpClientUtils;

public class ShopOaSessionUtil {
	
	private static String OACOOKIENAME = "PHPSESSID";
	public static String SHOPOAATTR = "shopoauser";
	
	public static String SHOPOA_USER = "shopoauser";
	public static String SHOPOA_ROLE = "shopoarole";
	public static String SHOPOA_MENU = "shopoamenu";
	
	private static CommonService commonService = SpringContextHolder.getBean(CommonService.class);
	
	public static String APPPATH = "/SperaChildrenPark/spera";
	
	private static String permission="{'status':'1','msg':'','dataset':[{'LOGIN_USER_ID':'WV00000069',"+
			"'LOGIN_USER_ACCOUNTS':'内部系统管理员','LOGIN_USER_NAME':'内部系统管理员',"+
			"'LOGIN_USER_PRIV':'32','LOGIN_DEPT_ID':'28','LOGIN_DEPT_NAME':'广州旗舰店',"+
			"'FILIALE_DEPT_ID':'27','FILIALE_DEPT_NAME':'广州蒂芭卜','SHOP_DEPT_ID':'28',"+
			"'SHOP_DEPT_NAME':'广州旗舰店'}]}";

	/**
	 * LOGIN_DEPT_ID:13
	USER_SUB_PRIV:false
	SMS_LOGIN:0
	LOGIN_USER_ID:WV00000015
	LOGIN_USER_PRIV:5
	USBKEY_PIN:
	LOGIN_USER_NAME:童柏霖
	LOGIN_THEME:theme17
	LOGIN_USBKEY:0
	LOGIN_AVATAR:
	LOGIN_PASSWORD:$1$n73.kI3.$hYhkWC0bgcfP/C9p2qmnM/
	LOGIN_POST_PRIV:1
	LOGIN_USER_ACCOUNTS:童柏霖
	 * @param request
	 * @return
	 */
	public static Map<String,Object> checkOaSession(HttpServletRequest request){
		String cookie = CookieUtils.getCookie(request, OACOOKIENAME);
		System.out.println("cookie---------->"+cookie);

		Map<String, Object> param = new HashMap<String, Object>();

		param.put("phpsessid", cookie);
		String json=null;
		
		String isDevelopment = JedisCont.getProParm("isDevelopment");
		if(isDevelopment!=null&&isDevelopment.equals("1")){
			json=permission;
		}else{
			json = HttpClientUtils.httpPost(JedisCont.getProParm("ipad.user_url")+"/WWWphpsessid.php",param);
		}
		System.out.println(json);

		Map<String, Object> map = JSON.parseObject(json, new TypeReference<Map<String, Object>>(){});
		request.getSession().removeAttribute(SHOPOAATTR);
		request.getSession().setAttribute(SHOPOAATTR, map);
        return map;
	}
	
	
	/**
	 * ipad登录
	 * @param request
	 * @return
	 */
	public static Map<String,Object> checkOaUser(String username,String password){
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("username", username);
		param.put("password", password);
		String json = HttpClientUtils.httpPost(JedisCont.getProParm("ipad.user_url")+"/WWWloginipadwithshop.php",param);
		Map<String, Object> map = JSON.parseObject(json, new TypeReference<Map<String, Object>>(){});
        return map;
	}
	

	
	/**
	 * 验证用户及菜单权限
	 * @param request
	 * @return
	 * {
		  "status": "1",
		  "msg": "",
		  "dataset": [
		    {
		      "LOGIN_USER_ID": "WV00000033",
		      "LOGIN_USER_ACCOUNTS": "门店员工",
		      "LOGIN_USER_NAME": "门店员工",
		      "LOGIN_DEPT_ID": "30",
		      "LOGIN_DEPT_NAME": "销售部",
		      "FILIALE_DEPT_ID": "27",
		      "FILIALE_DEPT_NAME": "广州分公司",
		      "SHOP_DEPT_ID": "28",
		      "SHOP_DEPT_NAME": "旗舰店"
		    }
		  ]
		}
	 */
	@SuppressWarnings("unchecked")
	public static boolean checkOaPriv(HttpServletRequest request,String url){
		String cookie = CookieUtils.getCookie(request, OACOOKIENAME);
		System.out.println("cookie---------->"+cookie);
		Map<String, Object> param = new HashMap<String, Object>();

		param.put("phpsessid", cookie);				
		param.put("url", url);

		String json = null;		
		
		String isDevelopment = JedisCont.getProParm("isDevelopment");
		if(isDevelopment!=null&&isDevelopment.equals("1")){
			json=permission;
		}else{
			json = HttpClientUtils.httpPost(JedisCont.getProParm("ipad.user_url")+"/WWWphpsessid_url.php",param);
		}		
		System.out.println(json);	
		
		Map<String, Object> map = JSON.parseObject(json, new TypeReference<Map<String, Object>>(){});
		String status = ApplicationUtils.Object2String(map.get("status"));
		if("0".equals(status)){
			return false;
		}else{
			List<Map<String,Object>> l = (List<Map<String,Object>>)map.get("dataset");
			if(l!=null&&l.size()>0){
				Map<String,Object> m = l.get(0);
				OaUser oaUser = new OaUser();
				oaUser.setOa_user_id(ApplicationUtils.Object2String(m.get("LOGIN_USER_ID")))
					.setOa_user_accounts(ApplicationUtils.Object2String(m.get("LOGIN_USER_ACCOUNTS")))
					.setOa_user_name(ApplicationUtils.Object2String(m.get("LOGIN_USER_NAME")))
					.setUser_dept_id(ApplicationUtils.Object2String(m.get("LOGIN_DEPT_ID")))
					.setUser_dept_name(ApplicationUtils.Object2String(m.get("LOGIN_DEPT_NAME")))
					.setUser_compdept_id(ApplicationUtils.Object2String(m.get("FILIALE_DEPT_ID")))
					.setUser_compdept_name(ApplicationUtils.Object2String(m.get("FILIALE_DEPT_NAME")))
					.setUser_shopdept_id(ApplicationUtils.Object2String(m.get("SHOP_DEPT_ID")))
					.setUser_shopdept_name(ApplicationUtils.Object2String(m.get("SHOP_DEPT_NAME")));
				if(ApplicationUtils.isNotNull(oaUser.getUser_shopdept_id())){
					String kids_park_id = commonService.getKidParkIDByDeptID(oaUser.getUser_shopdept_id());
					if(ApplicationUtils.isNotNull(kids_park_id)){
						oaUser.setKids_park_id(kids_park_id);
					}
				}
				setOaUser(request,oaUser);
			}
			return true;
		}
	}
	
	public static void setOaUser(HttpServletRequest request,OaUser oaUser){
		request.getSession().removeAttribute(SHOPOA_USER);
		request.getSession().setAttribute(SHOPOA_USER, oaUser);
	}
	
	public static OaUser getOaUser(HttpServletRequest request){
		return (OaUser)request.getSession().getAttribute(SHOPOA_USER);
	}
	
	
	/**
	 * 授权用户信息
	 */
	public static class OaUser implements Serializable {

		private static final long serialVersionUID = 1L;
		
		private String oa_user_id; // 编号
		private String oa_user_accounts; // 登录名
		private String oa_user_name; // 姓名
		private String user_dept_id;//部门编号
		private String user_dept_name;
		private String user_compdept_id;//分公司编号
		private String user_compdept_name;
		private String user_shopdept_id;//门店编号
		private String user_shopdept_name;
		private String kids_park_id;//对应自己系统数据库门店编号
		
		public String getOa_user_id() {
			return oa_user_id;
		}
		public OaUser setOa_user_id(String oa_user_id) {
			this.oa_user_id = oa_user_id;
			return this;
		}
		public String getOa_user_accounts() {
			return oa_user_accounts;
		}
		public OaUser setOa_user_accounts(String oa_user_accounts) {
			this.oa_user_accounts = oa_user_accounts;
			return this;
		}
		public String getOa_user_name() {
			return oa_user_name;
		}
		public OaUser setOa_user_name(String oa_user_name) {
			this.oa_user_name = oa_user_name;
			return this;
		}
		public String getUser_dept_id() {
			return user_dept_id;
		}
		public OaUser setUser_dept_id(String user_dept_id) {
			this.user_dept_id = user_dept_id;
			return this;
		}
		public String getUser_dept_name() {
			return user_dept_name;
		}
		public OaUser setUser_dept_name(String user_dept_name) {
			this.user_dept_name = user_dept_name;
			return this;
		}
		public String getUser_compdept_id() {
			return user_compdept_id;
		}
		public OaUser setUser_compdept_id(String user_compdept_id) {
			this.user_compdept_id = user_compdept_id;
			return this;
		}
		public String getUser_compdept_name() {
			return user_compdept_name;
		}
		public OaUser setUser_compdept_name(String user_compdept_name) {
			this.user_compdept_name = user_compdept_name;
			return this;
		}
		public String getUser_shopdept_id() {
			return user_shopdept_id;
		}
		public OaUser setUser_shopdept_id(String user_shopdept_id) {
			this.user_shopdept_id = user_shopdept_id;
			return this;
		}
		public String getUser_shopdept_name() {
			return user_shopdept_name;
		}
		public OaUser setUser_shopdept_name(String user_shopdept_name) {
			this.user_shopdept_name = user_shopdept_name;
			return this;
		}
		public String getKids_park_id() {
			return kids_park_id;
		}
		public OaUser setKids_park_id(String kids_park_id) {
			this.kids_park_id = kids_park_id;
			return this;
		}
	}
}
